//分类页面
var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
    behaviors: [basePage, baseList],
    /**
     * 页面的初始数据
     */
    data: {
        checkMobile: false,
        checkwxAuth: false,
        reqUrl: '',
        reqData: {
            /**排序[sales:人气最高,sku:库存排序] */
            order: ''
        },
        catIndex: 1, //分类索引
        catData: [],
        /**颜色列表 */
        colorArr: [],
        /**选中的颜色id */
        color_id: 0,
        /**是否显示颜色选择面板 */
        colorShow: false,
        screenPopupShow:false,
        screenPopupTop:0,
        screenPopupMaxHeight:0,
        colorCardIndex:-1,
        warehouse:{}
    },
    colorBtn_click() {
        this.setData({
            screenPopupShow: !this.data.screenPopupShow
        })
    },
    cat_change(e) {
        // console.log("cat_change", e);
        if (e.detail == 0) {
            this.toOften()
        } else {
            this.setData({
                catIndex: e.detail,
                color_id: 0
            });
            wx.setStorageSync('catId', this.data.catData[e.detail].cate_id)
            this.init_pdc();
        }

    },
    search_click(e) {
        this.data.current_page = 0;
        this.data.reqData = {
            so: e.detail,
            cate_id: this.data.catData[this.data.catIndex].cate_id
        };
        this.req_items();
    },
    order_change(e) {
        this.data.reqData.order = e.currentTarget.dataset.order;
        this.setData({
            reqData: this.data.reqData,
            screenPopupShow: false
        });
        this.data.current_page = 0;
        this.req_items();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("options", options)
        let warehouse = JSON.parse(options.warehouse);
        wx.setNavigationBarTitle({
            title: '交易大厅(' + warehouse.title + ")",
        })
        this.setData({
            warehouse:warehouse
        })
        this.data.reqUrl = '/home/supplier/product/items?wh_id=' + warehouse.wh_id;
        let catArr = getApp().globalData.cateArr;
        console.log("catArr", catArr)
        if (!catArr || !catArr.length) { //没有分类缓存
            // 请求分类列表
            return this.request({
                url: this.data.host + '/home/product/cate?limit=1000'
            }, this.get_cate_ok);
        }
        this.setData({
            catData: catArr
        })
        console.log("catID", wx.getStorageSync("catId"))

        if (wx.getStorageSync("catId")) {
            var catId = wx.getStorageSync("catId")
            catArr.map((val, index) => {
                if (val.cate_id == catId) {
                    this.setData({
                        catIndex: index
                    })
                }
            })
        }
        this.init_pdc()
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (options) {

        

    },
    //渲染分类数据
    get_cate_ok(res) {
        //  console.log("get_cate_ok", res);
        res.data.unshift({
            title: '我的常购',
        });
        getApp().globalData.cateArr = res.data;
        this.setData({
            catData: res.data
        })
        if (res.data && res.data.length) {
            if (wx.getStorageSync("catId")) {
                var catId = wx.getStorageSync("catId")
                res.data.map((val, index) => {
                    if (val.cate_id == catId) {
                        this.setData({
                            catIndex: index
                        })
                    }
                })
            }
            this.init_pdc()
        } else {
            wx.showToast({
                title: '缺少产品数据',
                icon: 'none'
            })
        }


    },

    /**
     * 初始化产品数据
     */
    init_pdc() {
        // console.log("this.data.catIndex",this.data.catIndex)
        let data = this.data;
        // console.log("data.catData",data.catData);
        let order = this.data.reqData.order;
        this.data.reqData = {
            cate_id: data.catData[this.data.catIndex].cate_id,
            color_id: data.color_id,
            so: '',
            order: order
        }
        data.current_page = 0;

        this.req_items();
    },
    /**颜色点击选择点击 */
    chooseColor(e){
        this.setData({
            colorCardIndex:e.currentTarget.dataset.index
        })
    },
    color_click(e) {
        // console.log("color_click", e);
        if(this.data.colorCardIndex==-1){
            wx.showToast({
              title: '请选择颜色',
              icon:"none"
            })
            return;
        }
        this.setData({
            color_id: this.data.colorArr[this.data.colorCardIndex].color_id,
            screenPopupShow: false
        });
        this.init_pdc()
    },
    hideScreenPopup(){
        this.setData({
            screenPopupShow: false
        });
    },
    resetColorCard(){
        this.setData({
            color_id:0,
            colorCardIndex:-1,
            screenPopupShow: false
        })
        this.init_pdc()
    },
    pdc_click(e) {

        let pdc = e.currentTarget.dataset.pdc;

        console.log("pdc", pdc);
        wx.navigateTo({
            url: '/pages/cls/pdcList/Index?title=' + pdc.product_name + '&product_id=' + pdc.product_id+"&wh_id="+this.data.warehouse.wh_id,
        })
    },
    /**
     * 获得列表数据
     */
    get_items_ok(res) {
        this.data.pages = res.pages;
        this.data.current_page = res.current_page;
        console.log("get_items_ok", res);
        res.data.map((res, index, arr) => {
            this.init_val(res, index, arr)
        })
        let itemArr = this.data.itemArr;
        res.current_page == 1 ? itemArr = res.data : itemArr = itemArr.concat(res.data);
        this.setData({
            itemArr,
            colorArr: res.color_list
        });
        wx.stopPullDownRefresh({
            success: (res) => {},
        })
    },
    init_val(val, index, arr) {
        val.thumb = this.get_thumb(val.product_img);
    },
    //跳转常购清单
    toOften() {
        wx.navigateTo({
            url: '/pages/cls/often/List',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        setTimeout(() => {
            //获取筛选栏位置信息
            this.getRect('.dropdown').then(res => {

                // if (!res) {
                //     res = { bottom: 210 };
                // }
                if (!res) return;
                let screenPopupMaxHeight = wx.getSystemInfoSync().windowHeight - res.bottom;
                this.setData({
                    screenPopupTop: res.bottom,
                    screenPopupMaxHeight
                });
                console.log("screenPopupMaxHeight", screenPopupMaxHeight)
            });

        }, 1000);
    },
    //获取节点位置信息
    getRect(selector) {
        let that = this;
        return new Promise(function (resolve) {
            that.createSelectorQuery()
                .select(selector)
                .boundingClientRect()
                .exec(function (rect) {
                    if (rect === void 0) {
                        rect = [];
                    }
                    return resolve(rect[0]);
                });
        });
    },
    


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '源花采购',
            path: 'pages/region/goods/index?uid=' + +getApp().globalData.userInfo.uid,
            success: (res) => {
                console.log("转发成功", res);
            },
            fail: (res) => {
                console.log("转发失败", res);
            }
        }
    }
})